﻿
package extremefx.modules.seaDragon {
	import extremefx.IDisposable;
	import extremefx.geom.XPoint;
	import extremefx.ui.UIAlign;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;		

	/**
	 * @author Marcelo Volmaro
	 */
	internal final class Overlay implements IDisposable {
		internal var _element:DisplayObject;
		internal var _bounds:Rectangle;
		
		private var _scales:Boolean;
		private var _adjust:Function;
		private var _position:XPoint;
		private var _size:XPoint;

		public function Overlay(pElement:DisplayObject, pLocation:Rectangle, pPlacement:uint = 0) {
			_element = pElement;
		
			update(pLocation, pPlacement);
			
			_size = new XPoint(_bounds.width, _bounds.height);
			_position = new XPoint(pLocation.x, pLocation.y);
		}
		
		public function update(pLocation:Rectangle, pPlacement:uint):void {
			_scales = (pLocation.width + pLocation.height) != 0;

			_bounds = pLocation.clone();
			_adjust = _createAdjustmentFunction(pPlacement == 0 ? (UIAlign.TOP | UIAlign.LEFT) : pPlacement);
		}
		
		private function _createAdjustmentFunction(pPlacement:uint):Function {
			switch (pPlacement) {
				case UIAlign.TOP_LEFT:
					return function(position:XPoint, size:XPoint):void {};
					
				case UIAlign.TOP:
					return function(position:XPoint, size:XPoint):void {
						position.x -= size.x / 2;
					};
					
				case UIAlign.TOP_RIGHT:
					return function(position:XPoint, size:XPoint):void {
						position.x -= size.x;
					};
					
				case UIAlign.RIGHT:
					return function(position:XPoint, size:XPoint):void {
						position.x -= size.x;
						position.y -= size.y / 2;
					};
					
				case UIAlign.BOTTOM_RIGHT:
					return function(position:XPoint, size:XPoint):void {
						position.x -= size.x;
						position.y -= size.y;
					};
					
				case UIAlign.BOTTOM:
					return function(position:XPoint, size:XPoint):void {
						position.x -= size.x / 2;
						position.y -= size.y;
					};
					
				case UIAlign.BOTTOM_LEFT:
					return function(position:XPoint, size:XPoint):void {
						position.y -= size.y;
					};
					
				case UIAlign.LEFT:
					return function(position:XPoint, size:XPoint):void {
						position.y -= size.y / 2;
					};
					
				case UIAlign.MIDDLE_CENTER:
				default:
					return function(position:XPoint, size:XPoint):void {
						position.x -= size.x / 2;
						position.y -= size.y / 2;
					};
			}
		}
		
		public function dispose():void {
			if (_element && _element.parent) _element.parent.removeChild(_element); 
		}
		
		public function setBounds(pHolder:Sprite, pPosition:XPoint, pSize:XPoint):void {
			if (!(_element.parent == pHolder)) {
				pHolder.addChild(_element);
			}
			
			var dirty:Boolean = false;
			
			if (!pPosition.equals(_position)) {
				dirty = true;
				_position = pPosition;
			}
			
			if (!pSize.equals(_size)) {
				dirty = true;
				_size = pSize;
			}
			
			if (dirty){
				_adjust(_position, _size);
				_element.x = _position.x;
				_element.y = _position.y;
				
				if (_scales){
					_element.width = pSize.x;
					_element.height = pSize.y;
				}
			}
		}
	}
}
